/*
 * @Author: jiutian
 * @Date: 2023-02-01 15:48:06
 * @LastEditTime: 2023-02-01 15:49:14
 * @LastEditors:
 */
const moment = require("moment");
const chalk = require("chalk");

/**
 * @description 将驼峰命名转化为下划线链接
 */
exports.humpToUnderline = (val) => {
  if (typeof val != "string")
    return console.log(new Error(val + "不是字符串类型"));
  return val.replace(/([A-Z])/g, "_$1").toLowerCase();
};

/**
 * @description 将下划线转化为驼峰命名链接
 */
exports.UnderlineToHump = (val) => {
  if (typeof val != "string")
    return console.log(new Error(val + "不是字符串类型"));
  return val.replace(/\_([A-z])/g, (_, letter) => letter.toUpperCase());
};

exports.changeObjType = (obj = {}, type = "h") => {
  let data = {};
  Object.keys(obj).forEach((key) => {
    if (type == "h") {
      data[this.humpToUnderline(key)] = obj[key];
    } else {
      data[this.UnderlineToHump(key)] = obj[key];
    }
  });

  return data;
};

/**
 * @description 格式时间
 */
exports.formatTime = (date, type = "YYYY-MM-DD") => {
  return moment(date).format(type);
};
